package com.zfh.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseConnection {

	// 连库四大参
	private static final String driverClassName = "com.mysql.jdbc.Driver";
	private static final String url = "jdbc:mysql://localhost:3306/curdTestDB?characterEncoding=utf8";
	private static final String username = "root";
	private static final String password = "root";
	
	private Connection conn;
	private static DatabaseConnection dbc;
	static {
		try {
			dbc = new DatabaseConnection();
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
	}
	private DatabaseConnection() throws ClassNotFoundException, SQLException {
		Class.forName(driverClassName);
		this.conn = DriverManager.getConnection(url, username, password);
		
	}
	
	public static Connection getConn() throws ClassNotFoundException, SQLException {
		return dbc.conn;
	}
	
	
	/**
	 * 	<h1>本地测试
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		DatabaseConnection dbc = new DatabaseConnection();
		Connection conn = dbc.getConn();
		if (conn!=null) {
			System.out.println("get conn success.");
		} else {
			System.out.println("get conn failure.");
		}
		
	}
}
